﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SmsUI.DataBase;
using SMSCore;

namespace SmsUI
{
    public partial class BatchModifySetForm : DevExpress.XtraEditors.XtraForm
    {
        public BatchModifySetForm()
        {
            InitializeComponent();
            InitCheckBox();

        }
        Dictionary<string, DevExpress.XtraEditors.CheckEdit> checkEditList;
        private void InitCheckBox()
        {
            checkEditList = new Dictionary<string, CheckEdit>(42);
            this.groupBox1.SuspendLayout();
            int offsetX = 0;
            int offsetY = 0;
            for (int i = 0; i < 42; i++)
            {

                DevExpress.XtraEditors.CheckEdit checkEdit1 = new CheckEdit();
                ((System.ComponentModel.ISupportInitialize)(checkEdit1.Properties)).BeginInit();
                this.groupBox1.Controls.Add(checkEdit1);
                checkEdit1.Location = new System.Drawing.Point(11 + offsetX * 65 + 2, 21 + offsetY * 19 + 2);

                checkEdit1.Name = "checkEdit1";
                checkEdit1.Properties.Caption = "COM" + (i + 1).ToString();
                checkEdit1.Size = new System.Drawing.Size(65, 19);
                checkEdit1.TabIndex = 0;
                checkEditList.Add(checkEdit1.Properties.Caption, checkEdit1);
                offsetX++;
                if (i % 8 == 7)
                {
                    offsetX = 0;
                    offsetY++;
                }

            }
            this.ResumeLayout(true);
            foreach (CheckEdit check in checkEditList.Values)
            {
                ((System.ComponentModel.ISupportInitialize)(check.Properties)).EndInit();
            }
        }

        private void btnSucc_Click(object sender, EventArgs e)
        {
            InputNum win = new InputNum("成功条数：");
            win.ShowDialog();
            if (win.DialogResult != DialogResult.OK)
            {
                return;
            }
            int num = int.Parse(win.PhoneNum);
            foreach (CheckEdit check in checkEditList.Values)
            {
                if (check.Checked)
                {
                    foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                    {
                        if (check.Properties.Caption == row.PortNum)
                        {
                            row.SuccessCount = num;
                        }
                    }
                }
            }
            DbRules.Instance.UpdateSetInfo();
            DbRules.Instance.GetSetInfo().AcceptChanges();
        }

        private void btnFail_Click(object sender, EventArgs e)
        {
            InputNum win = new InputNum("失败条数：");
            win.ShowDialog();
            if (win.DialogResult != DialogResult.OK)
            {
                return;
            }
            int num = int.Parse(win.PhoneNum);
            foreach (CheckEdit check in checkEditList.Values)
            {
                if (check.Checked)
                {
                    foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                    {
                        if (check.Properties.Caption == row.PortNum)
                        {
                            row.FailCount = num;
                        }
                    }
                }
            }
            DbRules.Instance.UpdateSetInfo();
            DbRules.Instance.GetSetInfo().AcceptChanges();

        }

        private void btnRevice_Click(object sender, EventArgs e)
        {
            InputNum win = new InputNum("接收条数：");
            win.ShowDialog();
            if (win.DialogResult != DialogResult.OK)
            {
                return;
            }
            int num = int.Parse(win.PhoneNum);
            foreach (CheckEdit check in checkEditList.Values)
            {
                if (check.Checked)
                {
                    foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                    {
                        if (check.Properties.Caption == row.PortNum)
                        {
                            row.ReciveCount = num;
                        }
                    }
                }
            }
            DbRules.Instance.UpdateSetInfo();
            DbRules.Instance.GetSetInfo().AcceptChanges();
        }

        private void btnUpLine_Click(object sender, EventArgs e)
        {
            InputNum win = new InputNum("发送上限：");
            win.ShowDialog();
            if (win.DialogResult != DialogResult.OK)
            {
                return;
            }
            int num = int.Parse(win.PhoneNum);
            foreach (CheckEdit check in checkEditList.Values)
            {
                if (check.Checked)
                {
                    foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                    {
                        if (check.Properties.Caption == row.PortNum)
                        {
                            row.MaxCount = num;
                        }
                    }
                }
            }
            DbRules.Instance.UpdateSetInfo();
            DbRules.Instance.GetSetInfo().AcceptChanges();
        }

        private void btnBtl_Click(object sender, EventArgs e)
        {
            InputNum win = new InputNum("波特率：");
            win.ShowDialog();
            if (win.DialogResult != DialogResult.OK)
            {
                return;
            }
            string num = win.PhoneNum;
            foreach (CheckEdit check in checkEditList.Values)
            {
                if (check.Checked)
                {
                    foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                    {
                        if (check.Properties.Caption == row.PortNum)
                        {
                            row.BTL = num;
                        }
                    }
                }
            }
            DbRules.Instance.UpdateSetInfo();
            DbRules.Instance.GetSetInfo().AcceptChanges();
        }

        private void btnSpeed_Click(object sender, EventArgs e)
        {
            InputNum win = new InputNum("速度：");
            win.ShowDialog();
            if (win.DialogResult != DialogResult.OK)
            {
                return;
            }
            int num = int.Parse(win.PhoneNum);
            foreach (CheckEdit check in checkEditList.Values)
            {
                if (check.Checked)
                {
                    foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                    {
                        if (check.Properties.Caption == row.PortNum)
                        {
                            row.Speed = num;
                        }
                    }
                }
            }
            DbRules.Instance.UpdateSetInfo();
            DbRules.Instance.GetSetInfo().AcceptChanges();
        }

        private void btnClearRemark_Click(object sender, EventArgs e)
        {
            if (DevExpress.XtraEditors.XtraMessageBox.Show(Properties.Resources.ConfirmInfo, "清空 ", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation).Equals(DialogResult.OK))
            {

                string num = "";
                foreach (CheckEdit check in checkEditList.Values)
                {
                    if (check.Checked)
                    {
                        foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                        {
                            if (check.Properties.Caption == row.PortNum)
                            {
                                row.Remark = num;
                            }
                        }
                    }
                }
                DbRules.Instance.UpdateSetInfo();
                DbRules.Instance.GetSetInfo().AcceptChanges();

            }
        }

        private void btnSppedType_Click(object sender, EventArgs e)
        {
            SelectSpeedType win = new SelectSpeedType();
            win.ShowDialog();
            if (win.DialogResult != DialogResult.OK)
            {
                return;
            }

            foreach (CheckEdit check in checkEditList.Values)
            {
                if (check.Checked)
                {
                    foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                    {
                        if (check.Properties.Caption == row.PortNum)
                        {
                            row.SetType = win.SpeedType;
                        }
                    }
                }
            }
            DbRules.Instance.UpdateSetInfo();
            DbRules.Instance.GetSetInfo().AcceptChanges();
        }

        private void btnSeleAll_Click(object sender, EventArgs e)
        {
            foreach (CheckEdit check in checkEditList.Values)
            {
                check.Checked = true;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ModifyMaTypeForm mf = new ModifyMaTypeForm();
            int maType = 0;
            if (mf.ShowDialog() == DialogResult.OK)
            {
                maType = mf.MaType;
            }

            foreach (CheckEdit check in checkEditList.Values)
            {
                if (check.Checked)
                {
                    foreach (smsdbDataSet.tb_SetRow row in DbRules.Instance.GetSetInfo())
                    {
                        if (check.Properties.Caption == row.PortNum)
                        {
                            row.MaType = maType.ToString();
                        }
                    }
                }
            }
            DbRules.Instance.UpdateSetInfo();
            DbRules.Instance.GetSetInfo().AcceptChanges();
        }

        private void btnPort_Click(object sender, EventArgs e)
        {
            if (DevExpress.XtraEditors.XtraMessageBox.Show("该操作将按照系统检测的端口设置端口名！将会清空所有已设置的端口名", "确认 ", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation).Equals(DialogResult.OK))
            {
                string[] ports = ComHelper.Instance.GetAvailComPorts();
                smsdbDataSet.tb_SetDataTable tb = DbRules.Instance.GetSetInfo();
                for (int i = 0; i < ports.Length; i++)
                {
                    tb[i].PortNum = ports[i];
                }
                DbRules.Instance.UpdateSetInfo();
                DbRules.Instance.GetSetInfo().AcceptChanges(); 
            }
        }
    }
}